package middleware

import (
	"api/internal/types"
	"context"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
)

type AuthTokenMiddleware struct {
}

func NewAuthTokenMiddleware() *AuthTokenMiddleware {
	return &AuthTokenMiddleware{}
}

func (m *AuthTokenMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation

		token := r.Header.Get("token")

		if token == "" {
			res := &types.RegMsgResponse{
				Code: -111111,
				Msg:  "token为空",
				Data: nil,
			}
			httpx.OkJsonCtx(r.Context(), w, res)
			return
		}
		//token效验
		//parseToken, err := pkg.ParseToken(token)
		//if err != nil {
		//	res := &types.RegMsgResponse{
		//		Code: -222222,
		//		Msg:  err.Error(),
		//		Data: nil,
		//	}
		//	httpx.OkJsonCtx(r.Context(), w, res)
		//	return
		//}
		//上下文传输userid
		userId := 1
		con := context.WithValue(r.Context(), "userId", userId)
		// Passthrough to next handler if need
		next(w, r.WithContext(con))
	}
}
